package com.atcx.controller;


import com.atcx.pojo.User;
import com.atcx.pojo.Warning;
import com.atcx.service.UserService;
import com.atcx.service.WarningService;
import com.atcx.util.PageResult;
import com.atcx.util.QueryPageBean;
import com.atcx.util.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pangShu
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/warning")
public class WarningController {



    @Resource
    private UserService userService;
    private Result result;
    @Resource
    private WarningService warningService;
    //添加
    @RequestMapping("add")
    public Result add(@RequestBody Warning warning){
        int i = warningService.add(warning);
        if (i>0){
            result = new Result(true,"添加信息成功");
        }else {
            result = new Result(false,"添加信息失败");
        }
        return result;
    }
    //修改
    @RequestMapping("update")
    public Result update(@RequestBody Warning warning){
        int i = warningService.updateWarning(warning);
        if (i>0){
            result = new Result(true,"修改信息成功");
        }else {
            result = new Result(false,"修改信息失败");
        }
        return result;
    }
    //删除
    @RequestMapping("delete")
    public Result delete(int id){
        int i = warningService.delete(id);
        if (i>0){
            result = new Result(true,"删除信息成功");
        }else {
            result = new Result(false,"删除信息失败");
        }
        return result;
    }
    //查询
    @RequestMapping("findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = warningService.findByPage(queryPageBean);
        if (page != null){
            result = new Result(true,"查询信息成功",page);
        }else {
            result = new Result(false,"查询信息失败");
        }
        return result;
    }
    //学生端
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean,HttpServletRequest request){
        Integer id = getUId(request);
        User user = userService.getUserById(id);
        if (user!=null){
            queryPageBean.setQueryString(user.getName());
            PageResult page = warningService.findByPage(queryPageBean);
            if (page.getRows().size()>0){
                result = new Result(true,"查询信息成功",page);
            }else {
                result = new Result(false,"查询信息失败");
            }
        }else {
            result = new Result(false,"查询信息失败");
        }
        return result;
    }
    public  static Integer getUId(HttpServletRequest request){
        Integer UserId = 0;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("userId")){
                String value = cookie.getValue();
                UserId = Integer.parseInt(value);
                break;
            }
        }
        return UserId;
    }
}

